﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

namespace SLG_Demo_0
{
	/// <summary>
	/// 完整的地图对象，包含数据层和显示层。一个 MapObject 表示一张地图
	/// </summary>
	public class MapObject : MonoBehaviour
	{
        /// <summary>
        /// 需要指定的 UnityEngine.Tilemaps.Tilemap 地图对象
        /// </summary>
        public Tilemap m_TileMap = null;

        /// <summary>
        /// 需要指定的，当前地图格子显示用的精灵
        /// </summary>
        public Sprite m_Sprite = null;

        /// <summary>
        /// 需要指定的，地图宽高
        /// </summary>
        public int m_width = 0, m_height = 0;

        /// <summary>
        /// 需要指定的，是否显示寻路路径
        /// </summary>
        public bool m_bShowSearch = false;

        ////////////////////////////////////////////////////////////
        /// <summary>
        /// 地图ID
        /// </summary>
        public int MapID { get; set; }

        /// <summary>
        /// 地图数据层
        /// </summary>
        public MapData MapData { get; private set; }

        /// <summary>
        /// 地图显示层
        /// </summary>
        public MapDisplay MapDisplay  { get; private set; }

		////////////////////////////////////////////////////////////
		public void CreateMap()
		{
			// 创建数据层
			MapData = new MapData();
			MapData.Create(this, m_width, m_height);

			// 创建显示层
			MapDisplay = new MapDisplay(m_TileMap, m_Sprite);
			MapDisplay.Create(this, m_width, m_height);

			// 测试地图：创建
			/*MapData = new MapData();
			MapData.Create(this, 8, 8);
			m_MapDisplay = new MapDisplay(m_TileMap, m_Sprite);
			m_MapDisplay.Create(this, 8, 8);*/
			RefreshInitMap();
		}

		// 重新刷新地图颜色和数据
		public void RefreshInitMap()
		{
			// 测试地图：设置地形
			int[,] swampAxis = new int[,]
			{
				{0, 2},
				{3, 3},
			};
			int[,] obstacleAxis = new int[,]
			{
				//{1, 0},
				{7, 7},
			};

			for (int iSwamp = 0; iSwamp < swampAxis.GetLength(0); ++iSwamp)
			{
				int x = swampAxis[iSwamp, 0];
				int y = swampAxis[iSwamp, 1];

				MapData.Cells[x, y].DataType = ECellDataType.Swamp;
				MapDisplay.Cells[x, y].DisplayType = ECellDisplayType.Swamp;
			}

			for (int iObstacle = 0; iObstacle < obstacleAxis.GetLength(0); ++iObstacle)
			{
				int x = obstacleAxis[iObstacle, 0];
				int y = obstacleAxis[iObstacle, 1];

				MapData.Cells[x, y].DataType = ECellDataType.Obstacle;
				MapDisplay.Cells[x, y].DisplayType = ECellDisplayType.Obstacle;
			}
		}

		public void DestroyMap()
		{
			//
		}

		void Start()
		{
			CreateMap();

			Object[] resObjs = Resources.LoadAll("Prefabs");
			for (int i = 0; i < resObjs.Length; i++)
			{
				Debug.Log("resObjs[" + i + "]" + resObjs[i].name);
			}

            // 创建测试单位预设体
            GameObject testUnit_0 = Resources.Load<GameObject>("Prefabs/TestUnit_0");
            if (testUnit_0 != null)
			{
				Vector3 pos = MapDisplay.CellToWorld(2, 3);
				Instantiate(testUnit_0, pos, Quaternion.identity);
			}

            GameObject testUnit_1 = Resources.Load<GameObject>("Prefabs/TestUnit_0");
            if (testUnit_1 != null)
			{
				Vector3 pos = MapDisplay.CellToWorld(0, 0);
				Instantiate(testUnit_1, pos, Quaternion.identity);
			}
		}

		void Update()
		{
			if (MapDisplay != null)
			{
				MapDisplay.Update();
			}
		}
	}
}
